<template>
	<view class="apps">
		<view class="flex ptb20 plr20 bgwhite mb20 br15 aic">
			<view class="f1 flex f14 black">总控开关</view>
			<view class="flex">
				<view class="f13 ptb20 plr20 white mr20 br15" style="background-color:#a82022;" @click="ctrlAll('ON')">全开</view>
				<view class="f13 ptb20 plr20 br15"  style="background-color:#bebebe;color:#444;" @click="ctrlAll('OFF')">全关</view>
			</view>
		</view>
		<uv-grid :col="4" @click="click">
			<uv-grid-item v-for="(item,index) in list" :key="index">
				<view class="flex fdc ptb20 plr30 mlr10 mtb10 bgwhite aic jcc br15">
					<uv-avatar :text="item.opened==0?`关`:`开`" :bgColor="item.opened==0?`#bebebe`:`#a82022`" :color="item.opened==0?`#444`:`#fff`" ></uv-avatar>
					<text class="f13 pt20">{{item.title}}</text>
				</view>
			</uv-grid-item>
		</uv-grid>
	</view>
</template>

<script>
	let app;
	export default {
		data() {
			return {
				is_login:0,
				list:[]
			}
		},
		onLoad() {
			app = this;
			this.UserInfo();
		},
		onShow(){
			if(this.is_login == 0){
				this.UserInfo()
			}
			
		},
		methods: {
			ctrlAll(status){
				let txt = status=="ON"?"打开":"关闭";
				uni.showModal({
					content: '确定要全部'+txt+'吗',
					success: (res) => {
						if (res.confirm) {
							this.request({
								url:"/lighting/all",
								data:{status:status},
								load:1,
								success:function(rlt){
									if(rlt.code == 1){
										app.getList();
										app.alert("已全部"+txt);
									}
								}
							})
						}
					}
				})
			},
			click(index){
				const item = this.list[index];
				this.request({
					url:"/lighting/edit",
					data:{id:item.id,opened:Math.abs(parseInt(item.opened)-1)},
					load:1,
					success:function(rlt){
						if(rlt.code == 1){
							let text = item.opened == 0?'已打开':'已关闭';
							app.getList();
							app.alert(item.title+text);
						}
					}
				})
			},
			async UserInfo(){
				const data = await this.request({url:"/index"});
				if(data.code != 1){
					uni.navigateTo({
						url:"/pages/login/login",
					})
					return ;
				}
				this.is_login =1;
				this.getList();
			},
			async getList(){
				const data = await this.request({url:"/lighting/index"});
				if(data.code ==1){
					this.list = data.data;
				}
			}
		}
	}
</script>

<style>
	
</style>
